package com.swn.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swn.common.util.DbHelper;


/**
 * 菜单管理的类
 * @author 董士明
 * @date 2018-01-11
 *
 */
@Service("menuService")
public class MenuService {
	private static Logger logger = Logger.getLogger("MenuService.class");
	@Autowired
	DbHelper dbHelper;
	/**
	 * 菜单权限
	 * 
	 */
	public List<Map<String, Object>> queryMenuByRoleID(String roleID) throws Exception {
		List<Map<String, Object>> menuList=null;
		if(roleID!=null && !roleID.equals("")){
			String roleId="(";  //组装用户对应的角色id 多个用逗号分隔
			for(String str:roleID.split(",")){
				if(roleId.equals("(")){
					roleId+="'"+str+"'";
				}else{
					roleId+=",'"+str+"'";
				}
			}
			roleId+=")";
			StringBuilder sql = new StringBuilder();
			sql.append(" select scm.id,scm.parent_id,scm.name,scm.img_url as icon,scm.menu_url as href,scm.final_node,scrw.role_id as roleId,concat('M',scm.id) as 'data-pjax' ")
					.append(" from shop_cm_menu scm ")
					.append(" left join shop_cm_role_warrant scrw on scm.id=scrw.menu_id ")
					.append(" where scrw.role_id in ").append(roleId);
			menuList= dbHelper.queryForList(sql.toString(), null);
		}
		return menuList;
	}

	
	/**
	 * 查询菜单
	 * @return
	 */
	public List<Map<String, Object>> queryMenuList()throws Exception{
		StringBuilder sql = new StringBuilder();
		sql.append("   select r.id,r.name,r.menu_url,r.img_url,r.parent_id pId,r.serial_num,r.final_node,r.menu_warrant,r.menu_warrant_name,r.create_user_id,r.create_time,r.update_user_id,r.last_update_time ")
		   .append("   ,ifnull((")
		   .append("      select max(1)  from shop_cm_menu where parent_id=r.id")
		   .append("   ),0)haschild")
		   .append("   from shop_cm_menu r  order by r.parent_id,serial_num")
		   .append("");
		return dbHelper.queryForList(sql.toString(), null);
	}
	
	/**
	 * 查询具体功能列表
	 * @return
	 */
	public List<Map<String, Object>> queryWarrantList()throws Exception{
		StringBuilder sql = new StringBuilder();
		sql.append("   select id,name from shop_cm_menu_features  order by name");
		return dbHelper.queryForList(sql.toString(), null);
	}
	
	/**
	 * 添加,修改菜单
	 * @param paramMap
	 * @return
	 */
	public boolean saveMenu(Map<String,Object> menuMap)throws Exception{
		boolean tag=false;
		try{
			 if(menuMap.get("id").equals("")){ //add
				 menuMap.put("id", null);
				 dbHelper.saveMap(menuMap,"shop_cm_menu");   //保存菜单授权信息
			 }else{ //update
				 dbHelper.updateMap(menuMap,"shop_cm_menu");   //保存菜单授权信息
			 }
			tag=true;
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw e;
		}
		return tag;
	}
	
	/**
	 *  根据id查询菜单
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	public Map<String, Object> queryMenuMapById(String id) throws Exception {
		StringBuilder sql = new StringBuilder();
		sql.append("   select r.id,r.name,r.menu_url,r.img_url,r.parent_id,r.final_node ,r.serial_num,r.menu_warrant,r.menu_warrant_name,r.create_user_id,r.create_time,r.update_user_id,r.last_update_time ")
		   .append("   from shop_cm_menu r where id=").append(id)
		   .append("    order by r.parent_id,serial_num");
		return dbHelper.queryForMap(sql.toString());
	}
	
	
	
	/**
	 * 删除菜单
	 * @param id
	 * @return
	 */
	public boolean deleteMenu(String id)  throws Exception {
		boolean tag=false;
		try{
			Map<String, Object> keyMap = new HashMap<>();
			keyMap.put("id", id);
			int delete = dbHelper.delete(keyMap, "shop_cm_menu");
			tag=delete>0?true:false;
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw e;
		}
	    return tag;
	}
	/**
	 * 查询需要删除的菜单是否有子菜单
	 * @param id
	 * @return
	 */
	public boolean querySubMenu(String id)  throws Exception {
		boolean tag=false;
		try{
			StringBuffer sql=new StringBuffer();
			sql.append("select 1  from shop_cm_menu where parent_id=").append(id);
			List<Map<String, Object>> sublist=dbHelper.queryForList(sql.toString(), null);
			if(sublist!=null && sublist.size()>0){
				tag=true;
			}
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw e;
		}
	    return tag;
	}
	/**
	 * 查询需要删除的菜单是否被角色绑定
	 * @param id
	 * @return
	 */
	public boolean queryBingByRole(String id)  throws Exception {
		boolean tag=false;
		try{
			StringBuffer sql=new StringBuffer();
			sql.append("select 1  from shop_cm_role_warrant where menu_id=").append(id);
			List<Map<String, Object>> sublist=dbHelper.queryForList(sql.toString(), null);
			if(sublist!=null && sublist.size()>0){
				tag=true;
			}
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw e;
		}
	    return tag;
	}
}
